var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    navList: [],
    categoryList: {},
    currentCategory: {},
    scrollLeft: 0,
    scrollTop: 0,
    goodsCount: 0,
    scrollHeight: 0,
    keyword: '',
  },
  onLoad: function (options) {
    this.getCatalog();
  },
  getCatalog: function () {
    //CatalogList
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });

    wx.request({
      url: api.CatalogList,
      success: function(res) {
        console.log(res);
        that.setData({
          navList: res.data.result,
          currentCategory: res.data.current_type,
        })
        wx.hideLoading();
      }
    })
    wx.request({
      url: api.GoodsCount,
      success: function(res) {
        that.setData({
          goodsCount: res.data.result
        })
      },
    })
  },
  getCurrentCategory: function (id) {
    let that = this;
    wx.request({
      url: api.CatalogCurrent,
      data:{
        id: id
      },
      method: 'GET',
      success: function(res) {
        that.setData({
          currentCategory: res.data.current_type,
          categoryList: res.data.type_list,
        })
        //console.log(that.data.categoryList)
      },
      
    })
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },

  switchCate: function (event) {
    var that = this;
    var currentTarget = event.currentTarget;
    if (this.data.navList[0].parentid == event.currentTarget.dataset.id) {
      return false;
    }

    this.getCurrentCategory(event.currentTarget.dataset.id);
  },

  bindInputKeyword: function (e) {
    this.setData({
      keyword: e.detail.value,
    })
  },

  // 搜索
  search: function () {
    var that = this;
    wx.showToast({
      title: '搜索中',
      icon: 'loading',
    });
    wx.navigateTo({
      url: '/pages/category/category?keyword=' + that.data.keyword,
    })
  }
})